package com.umeox.moto.common.utils;

import android.provider.Settings;
import android.util.Log;

import java.io.BufferedReader;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by Rambo 2019-12-10
 * Describe:
 */
public class DeviceManager {
    public static final String RAISE_UP_NODE = "/sys/class/input/input1/raisehand";
    public static final String OVERLAY_SCREEN_NODE = "/sys/touchpanel/poweroff";
    private volatile static DeviceManager instance;
    private static final String TAG = "DeviceManager";

    private DeviceManager() {

    }


    public static DeviceManager getInstance() {
        if (instance == null) {
            synchronized (DeviceManager.class) {
                if (instance == null) {
                    instance = new DeviceManager();
                }
            }
        }
        return instance;
    }

    public synchronized boolean isRaiseUpOn() {
        FileInputStream in = null;
        int value = 0;
        BufferedReader bReader = null;
        try {
            File fp = new File(RAISE_UP_NODE);
            in = new FileInputStream(fp);
            bReader = new BufferedReader(new InputStreamReader(in));
            String str = bReader.readLine();
            value = Integer.parseInt(str);
            Log.e(TAG, "RaiseUp value=" + value);
        } catch (Exception e) {
            Log.v(TAG, "RaiseUp value: ", e);
        } finally {
            close(in);
            close(bReader);
        }

        return value == 1;
    }

    public synchronized boolean isOverlayScreenOn() {
        FileInputStream in = null;
        int value = 0;
        BufferedReader bReader = null;
        try {
            File fp = new File(OVERLAY_SCREEN_NODE);
            in = new FileInputStream(fp);
            bReader = new BufferedReader(new InputStreamReader(in));
            String str = bReader.readLine();
            value = Integer.parseInt(str);
            Log.e(TAG, "OverlayScreen value=" + value);
        } catch (Exception e) {
            Log.v(TAG, "OverlayScreen value: ", e);
        } finally {
            close(in);
            close(bReader);
        }

        return value == 1;
    }


    public synchronized void setRaiseUpValue(boolean open) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(RAISE_UP_NODE);
            fw.write(open ? "1" : "0");
            fw.flush();
        } catch (Exception e) {
            Log.v(TAG, "setRaiseUpValue: ", e);
        } finally {
            close(fw);
            Settings.System.putInt(AppUtils.getApp().getContentResolver(), "raise_up", open ? 1 : 0);
        }
    }

    public synchronized void setOverlayScreenValue(boolean open) {
        FileWriter fw = null;
        try {
            fw = new FileWriter(OVERLAY_SCREEN_NODE);
            fw.write(open ? "1" : "0");
            fw.flush();
        } catch (Exception e) {
            Log.v(TAG, "setOverlayScreenValue: ", e);
        } finally {
            close(fw);
            Settings.System.putInt(AppUtils.getApp().getContentResolver(), "overlay_screen", open ? 1 : 0);
        }
    }

    /**
     * 关流
     *
     * @param in
     */
    private void close(Closeable in) {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}
